//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


namespace Shp.Runtime.Sitka.ServiceAgent.Factories
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Creates a new instance of type TTypeToBuild based on configuration. If there's no configuration, it creates TDefaultType
    /// </summary>
    /// <typeparam name="TTypeToBuild">The type to build</typeparam>
    /// <typeparam name="TDefaultType">The default type if there's no setting to TTypeToBuild</typeparam>
    public class ProviderFactory<TTypeToBuild, TDefaultType>
        where TDefaultType : class, TTypeToBuild, new()
        where TTypeToBuild : class
    {
        public TTypeToBuild Build(string settingsKey)
        {
            string typeName = System.Configuration.ConfigurationSettings.AppSettings[settingsKey];
            TTypeToBuild typeToBuild;

            if (string.IsNullOrEmpty(typeName))
            {
                return new TDefaultType();
            }
            else
            {
                typeToBuild = Activator.CreateInstance(Type.GetType(typeName)) as TTypeToBuild;
            }

            return typeToBuild;
        }
    }
}
